package cn.xfdzx.android.apps.shop.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.bean.HomeMarketSelectBeen;
import cn.xfdzx.android.apps.shop.presenter.HomeMarketPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.ShoppingCartAnimationView;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

/**
 * Creator: zf.
 * Date: 2020/7/28.
 * Description: 股东匠人，拾趣页面列表适配器.
 */
public class FineSelectXfdNewAdapter extends BaseQuickAdapter<HomeMarketSelectBeen.DataBean, BaseViewHolder> {
    private OnItemOnClickListener onClickCancleFocusListener;
    private List<HomeMarketSelectBeen.DataBean> mList;
    private HomeMarketPresenter marketPresenter;
    private Activity mContext;
    public FineSelectXfdNewAdapter(@Nullable Activity mContext,List<HomeMarketSelectBeen.DataBean> data,
                                   HomeMarketPresenter marketPresenter) {
        super(R.layout.item_fine_select_goods, data);
        this.mContext = mContext;
        this.marketPresenter = marketPresenter;
    }

    @Override
    public void setData(int index, @NonNull HomeMarketSelectBeen.DataBean data) {
        super.setData(index, data);
    }

    @Override
    protected void convert(final BaseViewHolder viewHolder, final HomeMarketSelectBeen.DataBean date) {
       // name = viewHolder.getView(R.id.tvName);
        TagTextView ttvContent = viewHolder.getView(R.id.tv_content);
        ImageView imageView = viewHolder.getView(R.id.iv_fine_select);
        TextView tvPriceBox = viewHolder.getView(R.id.tv_price_box);
        TextView tvCarNum = viewHolder.getView(R.id.tv_car_num);
        TextView tvSaleNum = viewHolder.getView(R.id.tv_sale_num);
        ImageView ivCar = viewHolder.getView(R.id.iv_car);
        TextView tvBgHui = viewHolder.getView(R.id.tv_bg_hui);
        View viewBgHui = viewHolder.getView(R.id.view_bg_hui);

        ImageGlideUtil.loadVideoImage(mContext,imageView,date.getThumb());


        //  头部标签
        if (null==date.getGoods_ensures()){
            ttvContent.setText(  date.getTitle());
        }else {
            ttvContent.setSingleTagAndContent(date.getGoods_ensures(), date.getTitle());
        }
        if (date.getUnit().isEmpty()) {
            if (Double.parseDouble(date.getDiscountprice()) == 0) {
                tvPriceBox.setText("¥" + date.getMarketprice());
            } else {
                tvPriceBox.setText("¥" + date.getDiscountprice());
            }
        } else {
            if (Double.parseDouble(date.getDiscountprice()) == 0) {
                tvPriceBox.setText("¥" + date.getMarketprice() + "/" + date.getUnit());
            } else {
                tvPriceBox.setText("¥" + date.getDiscountprice() + "/" + date.getUnit());
            }

        }
        tvCarNum.setText(date.getAdd_cart_num()+"");
        if (date.getAdd_cart_num()==0){
            tvCarNum.setVisibility(View.GONE);
        }else {
            tvCarNum.setVisibility(View.VISIBLE);
        }
        if (date.getIs_close() == 1||date.getStock()==0) {
            //已售罄
            ivCar.setImageResource(R.drawable.icon_home_pifa_che_hui);
            ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            viewBgHui.setVisibility(View.VISIBLE);
            tvBgHui.setVisibility(View.VISIBLE);
        } else {
            viewBgHui.setVisibility(View.GONE);
            tvBgHui.setVisibility(View.GONE);
            ivCar.setImageResource(R.drawable.icon_home_pifa_che_lv);
            ivCar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (EmptyUtil.isEmpty(APP_TOKEN)){
                        Intent intentlogin = new Intent(mContext, OneLoginMainActivity.class);
                        mContext.startActivity(intentlogin);
                        return;
                    }
                    ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(mContext);
                    shoppingCartAnimationView.setPiFaColor();
                    int postion[] = new int[2];
                    ivCar.getLocationInWindow(postion);
                    shoppingCartAnimationView.setStartPosition(new Point(postion[0], postion[1]));
                    ViewGroup rootView = (ViewGroup)mContext.getWindow().getDecorView();
                    rootView.addView(shoppingCartAnimationView);
                    shoppingCartAnimationView.setEndPosition(new Point( UtilsData.getPreference(mContext, "main_car").getInteger("tv_car_position_left"), UtilsData.getPreference(mContext, "main_car").getInteger("tv_car_position_right")));
                    shoppingCartAnimationView.startBeizerAnimation();
//                    marketPresenter.addCar("0","", date.getId()+"","1",viewHolder.getLayoutPosition());
                }
            });
        }
        tvSaleNum.setText("已售" + date.getSale_num() + "件");
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                //    mItemClickListener.onItemClick(position);
                }
            }
        });
    }



    public void addDatas(List<HomeMarketSelectBeen.DataBean>  list){
        super.addData(list);
        mData.addAll(list);
    }

    public void clearDatas(){
//        super.addData(list);
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<HomeMarketSelectBeen.DataBean> list){
//        clearDatas();
//        addDatas(list);
        this.mList = list;
        super.replaceData(list);
    }

    public void setItemOnClickListener(OnItemOnClickListener listener){
        this.onClickCancleFocusListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemClick(int position, List<HomeMarketSelectBeen.DataBean> children);
    }
}
